import {Injectable} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {environment} from '../../../environments/environment';

enum ORDERSTATUS {
  '全部' = '',
  '申请中' = 0,
  '待审批',
  '待支付',
  '待发货',
  '待收货',
  '待归还',
  '已到期',
  '归还中',
  '交易完成',
  '买断订单',
  '租金逾期',
  '机器逾期',
  '待赔付',
  '已取消',
}

@Injectable({
  providedIn: 'root'
})
export class HireDeviceService {
  serviceUrls: any = {
    dictUrl: '/dict/detail', // 字典查询
    uploadFileUrl: '/common/upload/file', // 上传

    commodityList: '/goodsRent/list', // 商品列表
    addCommodity: '/goodsRent/save', // 新增商品
    editCommodity: '/goodsRent/update', // 修改商品
    commodityDetail: '/goodsRent/details', // 商品详情
    commodityDelete: '/goodsRent/delete', // 删除商品
    commodityBatchStatus: '/goodsRent/batchStatus', // 批量上下架

    orderList: '/rentOrder/list', // 订单列表
    orderDetail: '/rentOrder/details', // 订单详情
    orderAudit: '/rentOrder/audit', // 订单审核
    deliverGoods: '/rentOrder/deliverGoods', // 发货
    cancelOrder: '/rentOrder/cancel', // 取消订单
    snapshot: '/rentOrder/snapshot', // 交易快照
    exportOrder: '/rentOrder/getOrderExcel', // 订单导出

    hotSearchList: '/hotWordRent/', // 热搜列表
    editHotSearch: '/hotWordRent/update', // 修改热搜
    addHotSearch: '/hotWordRent/save', // 添加热搜
    deleteHotSearch: '/hotWordRent/delete', // 删除热搜
    moveHotSearch: '/hotWordRent/move', // 移动热搜

    track: '/recycleFollower/save', // 跟进
    trackList: '/recycleFollower/list', // 跟进查询
  };
  ORDERSTATUS = ORDERSTATUS;
  orderStatus = [
    {id: '', name: '全部'},
    {id: 0, name: '申请中'},
    {id: 1, name: '待审批'},
    {id: 2, name: '待支付'},
    {id: 3, name: '待发货'},
    {id: 4, name: '待收货'},
    {id: 5, name: '待归还'},
    {id: 6, name: '已到期'},
    {id: 7, name: '归还中'},
    {id: 8, name: '交易完成'},
    {id: 9, name: '买断订单'},
    {id: 10, name: '租金逾期'},
    {id: 11, name: '机器逾期'},
    {id: 12, name: '待赔付'},
    {id: 13, name: '已取消'},
  ];
  PLAN: any = ['租完须归还', '租完可归还/买断', '租完即送'];
  ERRIMG = '';
  payStatus = ['未到期', '待结清（到了还款日）', '已结清', '已逾期'];

  constructor(public httpClient: HttpClient) {
  }

  // 文件上传
  fileUpload(file: any) {
    return this.post(this.serviceUrls.uploadFileUrl, file).toPromise();
  }

  // 跟进
  track(body: any) {
    return this.post(this.serviceUrls.track, body).toPromise();
  }

  // 跟进列表
  getTrackList(body: any) {
    return this.post(this.serviceUrls.trackList, body).toPromise();
  }

  getDict(key: string) {
    return this.httpClient.get('/dict/details?key=' + key, {}).toPromise();
  }

  // 获取商品列表
  getCommodityList(body: any) {
    return this.post(this.serviceUrls.commodityList, body).toPromise();
  }

  // 获取商品详情
  getCommodityDetail(id: any) {
    return this.post(this.serviceUrls.commodityDetail, {id}).toPromise();
  }

  // 添加商品
  addCommodity(body: any) {
    const url = body.id ? this.serviceUrls.editCommodity : this.serviceUrls.addCommodity;
    return this.post(url, body).toPromise();
  }

  // 删除商品
  commodityDelete(id: any) {
    return this.post(this.serviceUrls.commodityDelete, {id}).toPromise();
  }

  // 批量上下架
  commodityBatchStatus(body: any) {
    return this.post(this.serviceUrls.commodityBatchStatus, body).toPromise();
  }

  // 获取订单列表
  getOrderList(body: any) {
    return this.post(this.serviceUrls.orderList, body).toPromise();
  }

  // 获取订单详情
  getOrderDetail(id: any) {
    return this.post(this.serviceUrls.orderDetail, {id}).toPromise();
  }

  // 订单审核
  /*  Integer id ;
    Integer deductWay ;   扣款方式：0-首期款、1-首期款+补押金、2-首期款+预期扣款、3-首期款+补押金+预期扣款
    Integer wper;	预扣款期数*/
  orderAudit(body: any) {
    return this.post(this.serviceUrls.orderAudit, body).toPromise();
  }

  // 发货
  /* String expressName;   	物流公司名称
String expressCode;		物流公司编码
String expressNum;		物流单号
String imei;			产品IMEI号
Integer id;
String expressVas;		物流增值服务：0-无增值服务、1-电子微签、2-纸质回执单*/
  deliverGoods(body: any) {
    return this.post(this.serviceUrls.deliverGoods, body).toPromise();
  }

  // 取消订单
  cancelOrder(body: any) {
    return this.post(this.serviceUrls.cancelOrder, body).toPromise();
  }

  // 获取交易快照
  getSnapshot(id: any) {
    return this.post(this.serviceUrls.snapshot, {id}).toPromise();
  }

  // 订单导出
  exportOrder(params: any) {
    return this.httpClient.get(this.serviceUrls.exportOrder, {params, responseType: 'blob'}).toPromise();
  }

  // 热搜列表
  getHotSearchList(params: any) {
    return this.get(this.serviceUrls.hotSearchList, params).toPromise();
  }

  // 编辑热搜
  editHotSearch(body: any) {
    const url = body.id !== '' ? this.serviceUrls.editHotSearch : this.serviceUrls.addHotSearch;
    return this.post(url, {...body, type: 1}).toPromise();
  }

  // 删除热搜
  deleteHotSearch(body: any) {
    return this.post(this.serviceUrls.deleteHotSearch, body).toPromise();
  }


  // 移动热搜
  moveHotSearch(body: any) {
    return this.post(this.serviceUrls.moveHotSearch, body).toPromise();
  }

  get(url: any, params: any) {
    return this.httpClient.get(url, {params});
  }

  post(url: any, body: any) {
    return this.httpClient.post(url, body);
  }

  formatTime(date: any, type: 'date' | 'dateTime' | 'time') {
    if (!date || (typeof date) === 'string') {
      return date || '';
    }
    let result = '';
    switch (type) {
      case 'date':
        // @ts-ignore
        result = date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0];
        break;
      case 'dateTime':
        // @ts-ignore
        result = date.toISOString().match(/\d{4}-\d{2}-\d{2}/)[0] + ' ' + date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
        break;
      case 'time':
        // @ts-ignore
        result = date.toString().match(/\d{2}:\d{2}:\d{2}/)[0];
        break;
    }
    return result;
  }

}
